package flow;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

public class FlowStatistics {

    public static class FlowStatisticsMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] splits = value.toString().split("\t");
            String mobile = splits[1];
            long upFlow = Long.parseLong(splits[7]);
            long downFlow = Long.parseLong(splits[8]);
            FlowBean flowBean = new FlowBean(upFlow, downFlow);
            context.write(new Text(mobile), flowBean);
        }
    }

    public static class FlowStatisticsReducer extends Reducer<Text, FlowBean, Text, FlowBean> {
        @Override
        protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

            FlowBean flowBeanSum = new FlowBean();
            values.forEach(flowBean -> {
                flowBeanSum.setUpFlow(flowBean.getUpFlow() + flowBeanSum.getUpFlow());
                flowBeanSum.setDownFlow(flowBean.getDownFlow() + flowBeanSum.getDownFlow());
                flowBeanSum.setSumFlow(flowBean.getSumFlow() + flowBeanSum.getSumFlow());
            });
            context.write(key, flowBeanSum);
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//        System.setProperty("HADOOP_USER_NAME","root");
        Configuration configuration = new Configuration();
//        configuration.set("fs.defaultFS","hdfs://172.16.63.15:9000");
//        configuration.set("yarn.resourcemanager.hostname", "http://172.16.63.15:8032");
//        configuration.set("mapreduce.job.jar", "/home/student/wangbixiao/exe-hadoop-1.0-SNAPSHOT.jar");
//        configuration.set("mapreduce.framework.name", "yarn");
//        configuration.set("mapreduce.app-submission.cross-platform", "true");
        String[] otherArgs = new GenericOptionsParser(configuration, args).getRemainingArgs();
        if (otherArgs.length < 2) {
            System.err.println("Usage: FlowStatistics <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(configuration, "wangbixiao");
        job.setJarByClass(FlowStatistics.class);
        job.setMapperClass(FlowStatisticsMapper.class);
        job.setReducerClass(FlowStatisticsReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        for (int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
